package com.itheima.sevice.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MemberDao;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.dao.OrderDao;
import com.itheima.pojo.Member;
import com.itheima.pojo.Order;
import com.itheima.pojo.OrderSettingList;
import com.itheima.sevice.OrderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;

    @Autowired
    private MemberDao memberDao;

    /**
     * 分页查询订单
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Order> orderPage = orderDao.findPage(queryPageBean);

        List<OrderSettingList> orderSettingLists = new ArrayList<>();
        for (Order order : orderPage) {
            OrderSettingList orderSettingList = new OrderSettingList();
            BeanUtils.copyProperties(order, orderSettingList);
            Member member = memberDao.findById(order.getMemberId());
            if (member != null){
                orderSettingList.setName(member.getName());
                orderSettingList.setFileNumber(member.getFileNumber());
                orderSettingList.setPhoneNumber(member.getPhoneNumber());
            }

            orderSettingLists.add(orderSettingList);
        }

        return new PageResult(orderPage.getTotal(), orderSettingLists);
    }

    /**
     * 修改状态
     * @param order
     * @return
     */
    @Override
    public Integer updateOrderStatus(Order order) {
        Integer recordCount = orderDao.update(order);
        return recordCount;
    }
}
